HANDLE comport;
#endif
-#define debug 1
+#define debug 0
char * termread(char *ibuf, int size);
void termwrite(char *obuf, int size);
}
#endif
}
+
static void
mag_writeack(int osum)
{
gr = termread(ibuf, sizeof(ibuf));
- if (!gr && !got_version) {
- fatal("Magproto: No data received from GPS.\n");
+ if (!gr) {
+ if (!got_version) {
+ fatal("Magproto: No data received from GPS.\n");
+ } else {
+ if (is_file) {
+ found_done = 1;
+ }
+ return;
+ }
}
isz = strlen(ibuf);
fatal("Magproto: bad communication. Check bit rate.\n");
}
}
-if (debug)
-fprintf(stderr, "READ: %s\n", ibuf);
+ if (debug) {
+ fprintf(stderr, "READ: %s\n", ibuf);
+ }
if (IS_TKN("$PMGNCSM,")) {
last_rx_csum = strtoul(&ibuf[9], NULL, 16);
return;
}
if (IS_TKN("$PMGNVER,")) {
mag_verparse(ibuf);
-return;
+ return;
}
if (IS_TKN("$PMGNCMD,END") || is_file && feof(magfile_in)) {
found_done = 1;
-return;
+ return;
}
mag_writeack(isum);
{
int i=0;
DWORD cnt;
+
+ if (is_file) {
+ return fgets(ibuf, size, magfile_in);
+ }
+
ibuf[i]='a';
for(;i < size;i++) {
if (ReadFile (comport, &ibuf[i], 1, &cnt, NULL) != TRUE)
termwrite(char *obuf, int size)
{
DWORD len;
+
+ if (is_file) {
+ fwrite(obuf, size, 1, magfile_out);
+ return;
+ }
+
WriteFile (comport, obuf, size, &len, NULL);
if (len != size) {
- fatal("Write error");
+ fatal("Write error. Wrote %d of %d bytes.", len, size);
}
}
#else
#include <termios.h>
+
static struct termios orig_tio;
static void
terminit(const char *portname)
{
struct termios new_tio;
+ struct stat sbuf;
+
+ magfile_in = fopen(portname, "rb");
+
+ if (magfile_in == NULL) {
+ fatal("Magproto: Cannot open %s.%s\n",
+ portname, strerror(errno));
+ }
+
+ fstat(fileno(magfile_in), &sbuf);
+ is_file = S_ISREG(sbuf.st_mode);
+ if (is_file) {
+ icon_mapping = map330_icon_table;
+ got_version = 1;
+ return;
+ }
+
+ magfile_out = fopen(portname, "w+b");
+ magfd = fileno(magfile_in);
+
tcgetattr(magfd, &orig_tio);
new_tio = orig_tio;
new_tio.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|
mag_rd_init(const char *portname)
{
time_t now, later;
- struct stat sbuf;
-#if 0
- magfile_in = fopen(portname, "r");
-
- if (magfile_in == NULL) {
- fatal("Magproto: Cannot open %s.%s\n",
- portname, strerror(errno));
- }
- fstat(fileno(magfile_in), &sbuf);
- is_file = S_ISREG(sbuf.st_mode);
- if (is_file) {
- icon_mapping = map330_icon_table;
- got_version = 1;
- } else {
- terminit(portname);
- magfile_out = fopen(portname, "w+");
- magfd = fileno(magfile_in);
- }
-#else
-terminit(portname);
-is_file = 0;
-#endif
+ terminit(portname);
mag_handon();
now = time(NULL);
{
struct stat sbuf;
- magfile_out = fopen(portname, "w+");
+ magfile_out = fopen(portname, "w+b");
fstat(fileno(magfile_out), &sbuf);
is_file = S_ISREG(sbuf.st_mode);
if (is_file) {
- magfile_out = fopen(portname, "w+");
+ magfile_out = fopen(portname, "w+b");
} else {
mag_rd_init(portname);
}
return return_value;
}
-
-
/*
* Given an incoming waypoint messages of the form:
* $PMGNWPL,3549.499,N,08650.827,W,0000257,M,HOME,HOME,c*4D